﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.DialogViewModels/InputNextCrystalViewModel
* 类       名: InputNextCrystalViewModel
* 功      能:  TD
* 时      间:  2024-05-09 14:35:25
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;

namespace WaferSelectV3.ViewModels.DialogViewModels
{
    public class InputNextCrystalViewModel : BindableBase, IDialogAware
    {
        public InputNextCrystalViewModel()
        {
            OkCommand = new DelegateCommand(DoOk);
            CancelCommand = new DelegateCommand(DoCancel);
        }





        public DelegateCommand OkCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }

        private void DoOk()
        {
            if (TheoryCounts == 0)
            {
                MessageBox.Show("输入错误,理论数量不能为0", "录入信息错误提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (string.IsNullOrEmpty(CrystalNumber))
            {
                MessageBox.Show("晶棒编号不能为空", "晶棒信息错误提示框!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Regex regStart = new Regex("^[a-zA-Z]");
            if (!regStart.IsMatch(CrystalNumber))
            {
                MessageBox.Show("晶棒编号必须以字母开头!", "晶棒信息错误提示框!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            WorkingInfoEntity workinfoInput = new WorkingInfoEntity()
            {
                ProductionLot = LotNumber,
                PositionNumber = PositionNumber,
                FlowerNumber = FlowerNumber,
                CurrentCrystalNumber = CrystalNumber,
                TheoryCounts = TheoryCounts,
            };
            DialogParameters parameters = new DialogParameters()
            {
                { nameof(WorkingInfoEntity),workinfoInput }
            };
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
        }

        private void DoCancel()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, new DialogParameters()));
        }

        private string? _lotNumber;

        public string? LotNumber
        {
            get { return _lotNumber; }
            set
            {
                _lotNumber = value;
                RaisePropertyChanged();
            }
        }

        private string? _postionNumber;

        public string? PositionNumber
        {
            get { return _postionNumber; }
            set
            {
                _postionNumber = value;
                RaisePropertyChanged();
            }
        }

        private string? _flowerNumber;

        public string? FlowerNumber
        {
            get { return _flowerNumber; }
            set
            {
                _flowerNumber = value;
                RaisePropertyChanged();
            }
        }

        private string? _crystalNumber;

        public string? CrystalNumber
        {
            get { return _crystalNumber; }
            set
            {
                _crystalNumber = value;
                RaisePropertyChanged();
            }
        }

        private int _theoryCounts;

        public int TheoryCounts
        {
            get { return _theoryCounts; }
            set
            {
                _theoryCounts = value;
                RaisePropertyChanged();
            }
        }


        private string? _title;

        public string? Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged();
            }
        }

        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null)
            {
                parameters.TryGetValue("Title", out string title);
                parameters.TryGetValue("WorkingInfo", out WorkingInfoEntity workinginfo);
                if (!string.IsNullOrEmpty(title) && workinginfo != null)
                {
                    Title = title;
                    FlowerNumber = workinginfo.FlowerNumber;
                    LotNumber = workinginfo.ProductionLot;
                    CrystalNumber = $"ZY-{DateTimeHelper.GetYearMothDayFull()}_";
                    PositionNumber = workinginfo.PositionNumber;
                    TheoryCounts = 4000;
                }
            }
        }
    }
}
